package com.yt.al.object;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.TouchAction;

import java.util.List;

import org.apache.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import com.yt.al.util.WriteLogUtil;

public class BaseObject {
	protected  AppiumDriver driver;
	protected  WebDriverWait wait;
	protected  WebDriverWait waitPara;

	public BaseObject(AppiumDriver _driver)
	{
		driver = _driver;
	}

	/**
	 * 获取指定元素， 8秒超时
	 * @param by
	 * @return
	 */
	public WebElement getWebElement(By by){
		WebElement ele=null;
		try {
			wait=new WebDriverWait(driver, 8);
			ele=wait.until(ExpectedConditions.visibilityOfElementLocated(by));
		} catch (Exception e) {
			WriteLogUtil.writeMsgToFile("wait.until 获取元素超时");
		}
		return ele;	
	}
	
	/**
	 * 获取某个元素
	 * @param by
	 * @param s  ��ʱʱ������   �Զ���ȴ�ʱ��s,��ȴ�ʱ��Ϊs,����s����Null
	 * @return
	 */
	public WebElement getWebElement(By by,long s){
		WebElement ele=null;
		try {
			waitPara=new WebDriverWait(driver, s);
			ele=waitPara.until(ExpectedConditions.visibilityOfElementLocated(by));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
		}
		return ele;	
	}

	/**
	 * 获取某个元素
	 * @param by
	 * @return
	 */
		public WebElement getWebElementCheck(By by){
			WebElement ele=null;
			try {
				waitPara=new WebDriverWait(driver, 3);
				ele=waitPara.until(ExpectedConditions.visibilityOfElementLocated(by));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				//e.printStackTrace();
				WriteLogUtil.writeMsgToFile("wait.until 获取元素超时");
			}
			return ele;	
		}
	/**
	 * ��ȡ���Ԫ��,���ؼ���
	 * @param by
	 * @return
	 */
	public List<WebElement> getWebElements(By by){
		List<WebElement> list=null;
		try{
			list=driver.findElements(by);
			return list;
		}catch(Exception e){
			WriteLogUtil.writeMsgToFile("��ȡԪ��ʧ��:"+e);
			return null;
		}
	}

	/**
	 * 
	 * @param str
	 * @return
	 */
	public WebElement getEleByString(String str){
		return driver.findElementByAndroidUIAutomator(str);
	}
	/**
	 * 
	 * @param x ���  x
	 * @param y ���  y
	 */
	public void tapScreen(int x,int y){
		try{
			TouchAction action = new TouchAction(driver);
			action.tap(x, y+50).perform();//.tap(1,800,100,0);
		}catch(Exception e){
			Logger.getLogger("CheckResult : ").info("��� ("+x+","+y+")����쳣");
		}
	}


	public void press(int x,int y){
		try{
			TouchAction action = new TouchAction(driver);
			action.press(x, y);
		}catch(Exception e){
			Logger.getLogger("CheckResult : ").info("��� ("+x+","+y+")����쳣");
		}
	}
	
}
